<?php
/**
 * 封装
 * 
 */
 class Image
 {
 	//内存中的图片
 	private $image;
 	/**
	 * 图像基本信息
	 */
	 private $info;
 	public function __construct($src)
	{
		$info = getimagesize($src);
		$this->info = array(
			'width' => $info[0],
			'height' => $info[1],
			'type' => image_type_to_extension($info[2], false),
			'mime' => $info['mime']
		);
		$imagecreatefromtype = "imagecreatefrom{$this->info['type']}";
		$this->image = $imagecreatefromtype($src);
	}
	
	/**
	 * 封装创建一个新图像
	 * 图像输出
	 */
// 	public $imagecreatefromtype = "imagecreatefrom{$this->info['type']}";
//	public $imagetype = "image{$this->info['type']}";
	
	/**
	 * 操作图片 压缩
	 */
	 public function thumbnail($width, $height)
	 {
	 	$dst_image = imagecreatetruecolor($width, $height);
		imagecopyresampled($dst_image, $this->image, 0, 0, 0, 0, $width, $height, $this->info['width'], $this->info['height']);
		imagedestroy($this->image);
		$this->image = $dst_image;
	 }
	 
   /**
    * 添加文字水印
    */
    public function textMark($text, $tlocal, $color, $size=20, $angle=0, $alpha=50, $fontfile='SIMSUNB.TTF')
	{
		//参数过多可以通过数组传参
		$color = imagecolorallocatealpha($this->image, $color['red'], $color['green'], $color['blue'], $alpha);
		imagettftext($this->image, $size, $angle, $tlocal['x'], $tlocal['y'], $color, $fontfile, $text);
	}
	
	/**
	 * 添加图片水印
	 */
	public function imageMark($src_img, $local, $alpha=50)
	{
		$info = getimagesize($src_img);
		$type = image_type_to_extension($info[2], false);
		$imagecreatefromtype = "imagecreatefrom{$type}";
		$water = $imagecreatefromtype($src_img);
		imagecopymerge($this->image, $water, $local['x'], $local['y'], 0, 0, $info[0], $info[1], $alpha);
	}
	 /**
	  * 在浏览器中输出图片
	  */
	  public function show()
	  {
	  	header("Content-type: " .$this->info['type']);
		$imagetype = "image{$this->info['type']}";
		$imagetype($this->image);
	  }
	 
	   
	  /**
	   * 保存图片
	   */
	   public function save($newname)
	   {
		$imagetype = "image{$this->info['type']}";
		$imagetype($this->image, $newname.'.'.$this->info['type']);
	   }
	   
	  
	   /**
	    * 销毁图片
	    */
	    public function __destruct()
		{
			imagedestroy($this->image);
		}
 }
?>